# ifndef FILES_HH
# define FILES_HH
# include <iostream>
# include <fstream>
# include <cstdio>
# include <cstring>
# include <string>

std::string getLineOFile(std::string path, std::string mode, std::string lineNum);
int CountLines(std::string filename);
int CountLines(std::string filename)
{
    std::ifstream ReadFile;

    int n=0;
    char line[5120];
    ReadFile.open(filename.c_str(),std::ios::in);//ios::in 表示以只读的方式读取文件
    if(ReadFile.fail())//文件打开失败:返回0
    {
        return 0;
    }
    else//文件存在
    {
        while(!ReadFile.eof())
    {
        ReadFile.getline(line,5120,'\n');
        n++;
    }
        return n;
    }

    ReadFile.close();
}
std::string getLineOFile(std::string pathstr, std::string mode,int lineNum)
{
    std::FILE *file;
    char path[5120];
    strcpy( path, pathstr.c_str());
    int count;
    char trush[5120], resChar[5120];
    if (mode == "b" || mode == "begin") // 指begin 即从开头开始读
    {
        file = std::fopen(path, "r");
        for(count=lineNum-1; count!=0; count--)
	    {
		    fgets(trush, 5120, file);
		
	    }
        
        fgets(resChar, 5120, file);
        std::string resStr(resChar);
        return resStr;
    }
    else if(mode == "e" || mode == "end") // 值begin 即获取倒数第x行的
    {
        int lines = CountLines(path);
        int destLinenum = lines - lineNum +1;
        file = std::fopen(path, "r");
        for(count=destLinenum-1; count!=0; count--)
	    {
		    fgets(trush, 5120, file);
		
	    }
        
        fgets(resChar, 5120, file);
        std::string resStr(resChar);
        return resStr;
    }



}




# endif